package com.qiuzhao.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiuzhao.entity.Role;
import com.qiuzhao.entity.vo.CommonResult;
import com.qiuzhao.entity.vo.HistoryVO;
import com.qiuzhao.entity.vo.RoleVo;
import com.qiuzhao.mapper.RoleMapper;
import com.qiuzhao.service.ChatMemoryService;
import com.qiuzhao.service.RoleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ai")
public class ChatController {
    @Autowired
    private ChatClient chatClient;

    @Autowired
    private RoleService roleService;

    @Autowired
    private ChatMemoryService chatMemoryService;


    @GetMapping("/roleList")
    public CommonResult roleList() {
        CommonResult success = CommonResult.success(roleService.list().stream().map(role -> new RoleVo(role.getId(), role.getName(), role.getDescription())).toList());
        return success;
    }

    @RequestMapping(value = "/chat")
    public Flux<String> chat(@RequestParam String prompt,
                             @RequestParam String roleId,
                             @RequestParam String chatId) {
        Role role = roleService.getById(roleId);
        if (role == null)
            throw new RuntimeException("角色不存在");
        String systemPrompt = role.getSystemPrompt();
        chatId = chatId + "-" + roleId;
        String finalChatId = chatId;
        return chatClient
                .prompt()
                .system(systemPrompt)
                .user(prompt)
                .advisors(advisorSpec -> advisorSpec
                        .param(ChatMemory.CONVERSATION_ID, finalChatId)
                )
                .stream()
                .content();
    }

    @GetMapping("history/list")
    public CommonResult historyList() {

        List<HistoryVO> conversationIds = chatMemoryService.list().stream()
                .map(chatMemory ->{
                    HistoryVO historyVO = new HistoryVO();
                    historyVO.setConversationId(chatMemory.getConversationId());
                    historyVO.setTitle(chatMemory.getTitle());
                    return historyVO;
                }).distinct()
                .collect(Collectors.toList());

        return CommonResult.success(conversationIds);
    }

    @GetMapping("history/{conversationId}")
    public CommonResult history(@PathVariable String conversationId) {
        LambdaQueryWrapper<com.qiuzhao.entity.ChatMemory> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(com.qiuzhao.entity.ChatMemory::getConversationId, conversationId);
        lambdaQueryWrapper.orderByAsc(com.qiuzhao.entity.ChatMemory::getCreateTime);
        List<com.qiuzhao.entity.ChatMemory> chatMemoryList = chatMemoryService.list(lambdaQueryWrapper);
        return CommonResult.success(chatMemoryList);
    }
}
